<template>
<!--  添加按钮-->
  <el-button type="primary" @click="showServe" v-if="rid!=null">添加服务</el-button>
  <div>
<!--    table列表展示-->
    <el-table :data="tableData" style="width: 100%">
      <el-table-column align="center" prop="id" label="服务编号" />
      <el-table-column align="center" prop="name" label="服务名称" />
      <el-table-column align="center" prop="referencePrice" label="参考价格" width="100px"/>
      <el-table-column align="center" prop="price" label="区域价格" width="100px">
        <template #default="scope" >
          <td contenteditable="true" >{{scope.row.price}}</td>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="isHot" label="是否热门" width="150px">
        <template #default="scope">
          <el-tag v-if="scope.row.isHot==1" type="success">是</el-tag>
          <el-tag v-else type="danger">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="saleStatus" label="状态" width="150px">
        <template #default="scope">
          <el-tag v-if="scope.row.saleStatus==0" type="success">草稿</el-tag>
          <el-tag v-else-if="scope.row.saleStatus==1" type="danger">下架</el-tag>
          <el-tag v-else-if="scope.row.saleStatus==2" type="warning">上架</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="address" label="操作" >
        <template #default="scope">
          <el-button type="info" link @click="handleHot(scope.row)">
            <span v-if="scope.row.isHot==0">设置热门</span>
            <span v-if="scope.row.isHot==1" >取消热门</span>
          </el-button>
          <el-button type="danger" link @click="handleDelete(scope.row)">删除</el-button>
          <el-button type="text" link @click="handleSaleStatus(scope.row)">
            <span v-if="scope.row.saleStatus==0" >上架</span>
            <span v-if="scope.row.saleStatus==1">上架</span>
            <span v-if="scope.row.saleStatus==2">下架</span>
          </el-button>
          <el-button link type="primary" @click="showUpdateDialog(scope.row)">修改区域价格</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="currentPage4"
      v-model:page-size="pageSize4"
      :page-sizes="[1, 2, 3, 400]"
      :size="size"
      :disabled="disabled"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
<!--    添加服务弹窗-->
    <el-dialog
      v-model="dialogVisible"
      title="添加服务"
      width="700"
      :before-close="handleClose"
    >
      <el-form :model="saveForm" label-width="auto" style="max-width: 600px">
        <el-form-item label="服务类型：">
          <el-select v-model="serverType" placeholder="请选择服务类型" clearable @change="serverItemList(serverType)">
            <el-option
              v-for="item in typeData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
          <span style="color: red">清先选择服务类型</span>
        </el-form-item>
        <el-form-item label="服务项：" v-if="serverType!=null">
          <el-select v-model="saveForm.serveItemId" placeholder="请选择服务项" clearable>
            <el-option
              v-for="item in itemData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="价格：">
          <el-input v-model="saveForm.price" placeholder="请输入价格" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="saveFormSbt()">添加</el-button>
          <el-button type="primary" @click="dialogVisible = false">
            关闭
          </el-button>
        </div>
      </template>
    </el-dialog>
<!--    修改区域价格-->
    <el-dialog
      v-model="updateDialog"
      title="修改区域价格"
      width="500"
      :before-close="handleClose"
    >
      区域价格：
      <el-input v-model="updateForm.price" placeholder="请输入区域价格" clearable/>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="updateReferencePrice()">提交</el-button>
          <el-button type="primary" @click="updateDialog = false">
            取消
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import {
  deleteById,
  findAll, saveServe,
  serveItemListByTypeId,
  setHot,
  setSaleStatus,
  typeList, updatePrice
} from "@/api/regionalServices";
import {ElMessage} from "element-plus";
import {useRoute} from "vue-router";


const tableData = ref([])
const  route=useRoute();
const rid=route.query.regionId
const form=ref({
  pageSum:1,
  pageSize:4,
  regionId:rid
})
const total=ref()
// 查所有
function findList(){
  findAll(form.value).then(res=>{
    tableData.value=res.data.data.records
    console.log(res.data)
    total.value=res.data.data.total
  })
}
findList()
// 上下页
const handleSizeChange = (val) => {
  form.value.pageSize=val
  findList()
}
const handleCurrentChange = (val) => {
  form.value.pageSum=val
  findList()
}
// 删除
const handleDelete = (row) => {
  console.log(row)
  if (confirm('是否删除')) {
    if (row.saleStatus == 2) {
      ElMessage.info('该服务已上架，无法删除')
    } else {
      deleteById(row.id).then(res => {
        console.log(res)
        if (res.data.code=200){
          ElMessage.success('删除成功')
          findList()
        }else {
          ElMessage.error('删除失败')
        }
      })
    }
  }
}
// 设置热门和取消热门
const handleHot = (row) => {
  setHot(row.id).then(res => {
    if (res.data.code=200){
      ElMessage.success('设置成功')
      findList()
    }else {
      ElMessage.error('设置失败')
    }
  })
}
// 根据服务id设置上架/下架
const handleSaleStatus = (row) => {
  setSaleStatus(row.id).then(res => {
    if (res.data.code=200){
      ElMessage.success('设置成功')
      findList()
    }else {
      ElMessage.error('设置失败')
    }
})
}

const serverType = ref()
const dialogVisible = ref(false)
const saveForm = ref({
  serveItemId:null,
  regionId:rid,
  price:null
})
// 添加按钮
const showServe = () => {
  dialogVisible.value = true
  typeLists()
}
// 查看所有的服务类型
const typeData = ref([])
const typeLists = () => {
  typeList().then(res => {
    console.log(res.data)
    typeData.value = res.data.data
  })
}
// 根据服务类型的id查询所有的服务项
const itemData = ref([])
const serverItemList = (serverType) => {
  serveItemListByTypeId(serverType).then(res =>{
    itemData.value=res.data.data
  })
}
// 添加服务提交
const saveFormSbt = () => {
  console.log(saveForm.value)
  saveServe(saveForm.value).then(res => {
    if (res.data.code=200){
      ElMessage.success('添加成功')
      dialogVisible.value = false
      findList()
    }else {
      ElMessage.error('添加失败')
    }
  })
}
// 修改区域价格
const updateDialog = ref(false)
const updateForm = ref({
  id:null,
  price:null
})
const showUpdateDialog = (row) => {
  updateDialog.value = true
  updateForm.value.id=row.id
  updateForm.value.price=row.price
}
const updateReferencePrice = () => {
  console.log(updateForm.value)
  updatePrice(updateForm.value).then(res => {
    if (res.data.code=200){
      ElMessage.success('修改成功')
      updateDialog.value = false
      findList()
    }else {
      ElMessage.error('修改失败')
    }
  })
}
</script>
<style>
td {
  width: 100px;
  border: 1px solid black;
  text-align: center;
}
</style>
